package com.Jano.service.Impl;

import com.Jano.dao.Impl.UserDaoImpl;
import com.Jano.dao.UserDao;
import com.Jano.entity.User;
import com.Jano.exception.BusinessException;
import com.Jano.service.UserService;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.Objects;

/**
 * description: UserServiceImpl <br>
 * date: 2022/6/6 12:05 PM <br>
 * author: zoumaoji <br>
 * version: 1.0 <br>
 */
public class UserServiceImpl extends AbstractBaseServiceImpl implements UserService {
    UserDao userDao = new UserDaoImpl();

    @Override
    public User login(String userName, String passWord) {
        if (StringUtils.isBlank(userName)) {
            throw new BusinessException("用户名不能为空");
        }
        if (StringUtils.isBlank(passWord)) {
            throw new BusinessException("密码不能为空");
        }
        User userLogin = userDao.login(userName, DigestUtils.md5Hex(passWord));
        if (Objects.isNull(userLogin)) {
            throw new BusinessException("用户不存在");
        }
        return userDao.login(userName, passWord);
    }

    @Override
    public void register(String userName, String passWord, String pwdConfirm) {
        throwIfTrue(StringUtils.isBlank(userName), "用户名不能为空");
        throwIfTrue(userDao.isExist(userName), "用户名已存在");
        throwIfFalse(passWord.equals(pwdConfirm), "两次密码不相等");
        userDao.register(userName, passWord);
    }
}
